package com.kly.cashmall.module.repay;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.gson.JsonArray;
import com.kly.cashmall.R;
import com.kly.cashmall.base.BaseBarActivity;
import com.kly.cashmall.bean.QueryPayResultBean;
import com.kly.cashmall.event.HomeEvent;
import com.kly.cashmall.framework.mvp.PresenterLifeCycle;
import com.kly.cashmall.module.repay.presenter.RepayResultPresenter;
import com.kly.cashmall.module.repay.presenter.RepayResultViewer;
import com.kly.cashmall.utils.function.GlideUtils;
import com.kly.cashmall.widget.LoadingImageView;

import org.greenrobot.eventbus.EventBus;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


/**
 * @author
 */
public class RepayResultActivity extends BaseBarActivity implements View.OnClickListener, RepayResultViewer {

    private static final String PAYMENT_STATUS_HANDING = "HANDING";
    private static final String PAYMENT_STATUS_SUCCESS = "SUCCESS";
    private static final String PAYMENT_STATUS_CLOSED = "CLOSED";
    private static final String PAYMENT_STATUS_FAILURE = "FAILURE";
    private static final String TITLE_SUCCESS = "Repay Result";
    private static final String BILLNO = "billNo";
    @PresenterLifeCycle
    RepayResultPresenter presenter = new RepayResultPresenter(this);

    private ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();

    private String billNo;
    private int querySize = 0;
    private final static int totalQuerySize = 12;//轮询一分钟
    private ImageView mImageView;
    private TextView mResultInfoTitle;
    private TextView mResultInfoText;
    private TextView mButton;
    private JsonArray billNos;
    private LinearLayout mWait;
    private LinearLayout mSuccess;
    private LoadingImageView mImgQuery;
    private int type;

    @Override
    protected void setView(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_repay_result);
        billNo = getIntent().getStringExtra(BILLNO);
        type = getIntent().getIntExtra("type", -1);
        setTitle(TITLE_SUCCESS);
        mWait = findViewById(R.id.ll_waiting);
        mSuccess = findViewById(R.id.ll_success);
        mImageView = findViewById(R.id.repayment_result_successful_image);
        mResultInfoTitle = findViewById(R.id.repayment_result_successful_title);
        mResultInfoText = findViewById(R.id.repayment_result_successful_text);
        mButton = findViewById(R.id.repayment_result_button);
        mImgQuery = bindView(R.id.img_query);

        mButton.setOnClickListener(this);
    }

    @Override
    protected void loadData() {
        billNos = new JsonArray();
        billNos.add(billNo);
        presenter.queryPayStatus(billNos, type);
    }


    public static Intent callIntent(Context activity, String billNo, int type) {
        Intent intent = new Intent(activity, RepayResultActivity.class);
        intent.putExtra(BILLNO, billNo);
        intent.putExtra("type", type);
        return intent;
    }

    @Override
    public void onClick(View v) {
        finish();
    }

    @Override
    protected void willDestroy() {
        super.willDestroy();
        EventBus.getDefault().post(new HomeEvent());

    }

    @Override
    public void getRepayStatusSuccess(QueryPayResultBean bean) {
        if (PAYMENT_STATUS_HANDING.equals(bean.getRepaymentStatus())) {
            mWait.setVisibility(View.VISIBLE);
            mSuccess.setVisibility(View.GONE);
            mImgQuery.setLoadingStatus(true);
            scheduleQueryPayStatus();
        } else if (PAYMENT_STATUS_CLOSED.equals(bean.getRepaymentStatus())) {
            stopLoading(bean);
            if (!service.isShutdown()) {
                service.shutdownNow();
            }
        } else if (PAYMENT_STATUS_SUCCESS.equals(bean.getRepaymentStatus())) {
            stopLoading(bean);
            if (!service.isShutdown()) {
                service.shutdownNow();
            }
        } else if (PAYMENT_STATUS_FAILURE.equals(bean.getRepaymentStatus())) {
            stopLoading(bean);
            if (!service.isShutdown()) {
                service.shutdownNow();
            }
        }
    }

    private void stopLoading(QueryPayResultBean bean) {
        mWait.setVisibility(View.GONE);
        mSuccess.setVisibility(View.VISIBLE);
        mImgQuery.setLoadingStatus(false);
        GlideUtils.loadImage(this, bean.getRepaymentStatusImg(),mImageView);
        mResultInfoTitle.setText(bean.getRepaymentTitle());
        mResultInfoText.setText(bean.getRepaymentTips());
        mButton.setText(bean.getRepaymentButtonText());
    }


    private void scheduleQueryPayStatus() {
        if (querySize >= totalQuerySize) {
            if (!service.isShutdown()) {
                service.shutdownNow();
            }
        }
        service.schedule(new Runnable() {
            @Override
            public void run() {
                presenter.queryPayStatus(billNos, type);
                querySize++;
            }
        }, 5, TimeUnit.SECONDS);
    }
}
